/*
 * Project: Set Sample
 * Copyright (C) 2013 alf.labs gmail com,
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.alflabs.setsample.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import com.alflabs.annotations.Null;
import com.alflabs.setsample.R;
import com.alflabs.setsample.stats.GAHelper;

/**
 * Note: reverted from WebViewFragment to regular layout in Activity.
 * WebViewFragment is API 13+ and is not supported by the appcompat/support library.
 */
public class HelpActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle state) {
        super.onCreate(state);

        setContentView(R.layout.help_activity);
        WebView wv = (WebView) findViewById(R.id.webViewForActivity);
        initWebView(wv);

        // Show the Up button in the action bar.
        ActionBar ab = getSupportActionBar();
        if (ab != null) {
            ab.setDisplayHomeAsUpEnabled(true);
        }
    }

    /**
     * Initializes the web view.
     */
    private void initWebView(@Null WebView wv) {
        if (wv == null) return;
        wv.setVisibility(View.VISIBLE);
        wv.loadUrl("file:///android_asset/help.html");
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:

            Intent i = getIntent();
            String fromActivity = i == null ? null : i.getStringExtra("from_activity");

            if (SetBoardFragment.class.getName().equals(fromActivity)) {
                FragmentManager fm = getSupportFragmentManager();
                fm.popBackStack();
                finish();

            } else {
                // This ID represents the Home or Up button. In the case of this
                // activity, the Up button is shown. Use NavUtils to allow users
                // to navigate up one level in the application structure. For
                // more details, see the Navigation pattern on Android Design:
                //
                // http://developer.android.com/design/patterns/navigation.html#up-vs-back
                NavUtils.navigateUpFromSameTask(this);
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStart() {
        super.onStart();
        GAHelper.activityStart(this);  // GA
    }

    @Override
    protected void onStop() {
        super.onStop();
        GAHelper.activityStop(this);   // GA
    }
}
